

#include <sys/cdefs.h>
#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#else
#include <lib/libkern/libkern.h>
#endif

/* 64bit version is in strspn.c */
#if ULONG_MAX != 0xffffffffffffffffull

size_t
strcspn(const char *s, const char *charset)
{
	static const uint8_t idx[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
	const char *t;
	uint8_t set[32];
#define UC(a) ((unsigned int)(unsigned char)(a))

	_DIAGASSERT(s != NULL);
	_DIAGASSERT(charset != NULL);

	if (charset[0] == '\0')
		return strlen(s);
	if (charset[1] == '\0') {
		for (t = s; *t != '\0'; ++t)
			if (*t == *charset)
				break;
		return t - s;
	}

	(void)memset(set, 0, sizeof(set));

	for (; *charset != '\0'; ++charset)
		set[UC(*charset) >> 3] |= idx[UC(*charset) & 7];

	for (t = s; *t != '\0'; ++t)
		if (set[UC(*t) >> 3] & idx[UC(*t) & 7])
			break;
	return t - s;
}

#endif
